/*
@最初版本开发日期: 2009年08月26日
@著作权所有: Lou Barnes ( http://www.loubarnes.com )

报告漏洞，意见或建议, 请联系 Lou Barnes(iua1024@gmail.com) 或登陆 http://www.loubarnes.com
*/


var pagination = {
	
	m_iTotal : 0,
	m_iPageNo : 0,
	m_iMaxPageNo : 0,
	m_iOffset : 0,
	m_iLimit : 20,
	m_oData : [],
	
	m_sNavURL : '',
	m_sDataURL : '',
	
	m_fnUpdateNav : function(){return;},
	m_fnUpdateData : function(){return;},

	init : function()
	{
		
		var _self = this;
		$.ajax({
			type: "GET",
			url: _self.m_sNavURL,
			dataType: "json",
			success: function(json)
			{
				var iTotal = Number(json.total);
				_self.m_iTotal = iTotal;
				if( _self.m_iTotal%_self.m_iLimit == 0 ) _self.m_iMaxPageNo = _self.m_iTotal/_self.m_iLimit;
				else _self.m_iMaxPageNo = Math.floor( _self.m_iTotal/_self.m_iLimit ) +1;
				_self.m_iPageNo = 0;
				if(iTotal) _self.page(1);
				else _self.m_fnUpdateNav();
			}
		});
	},
	
	page : function(iPageNo)
	{
		if(iPageNo>0 && iPageNo<=this.m_iMaxPageNo)
			this.m_iPageNo = iPageNo;
		else
			return;
		this.m_iOffset = (this.m_iPageNo-1)*this.m_iLimit;
		this.m_fnUpdateNav();
		
		var _self = this;
		$.ajax({
			type: "GET",
			url: _self.m_sDataURL+"&limit="+_self.m_iLimit+"&offset="+_self.m_iOffset,
			dataType: "json",
			success: function(json){
				_self.m_fnUpdateData(json);
			}
		});
	},
	
	
	setLimit : function(iLimit){this.m_iLimit = iLimit;},	
	setUpdateNavFn : function(fn){this.m_fnUpdateNav = fn;},	
	setUpdateDataFn : function(fn){this.m_fnUpdateData = fn;},	
	setNavUrl : function(sUrl){this.m_sNavURL = sUrl;},
	setDataUrl : function(sUrl){this.m_sDataURL = sUrl;},
	
	getTotal : function(){return this.m_iTotal; },
	getPageNo : function(){ return this.m_iPageNo; },
	getMaxPageNo : function(){ return this.m_iMaxPageNo; },
	getLimitStart : function(){ return this.m_iOffset; },
	getLimit : function(){ return this.m_iLimit; }

}
